function messageBox(content,state) {
    toastr.options = {
        closeButton: false,
        debug: false,
        progressBar: false,
        positionClass: "toast-top-right",
        onclick: null,
        showDuration: "300",
        hideDuration: "1000",
        // timeOut: "1000",  
        extendedTimeOut: "1000",
        showEasing: "swing",
        hideEasing: "linear",
        showMethod: "fadeIn",
        hideMethod: "fadeOut"
    };
    if (content == null) {
        content = '';
    }
    var len = content.length;
    if (len <= 10 && len > 0) {
        toastr.options.timeOut = "1800";
    } else if (len <= 20) {
        toastr.options.timeOut = "2800";
    } else if (len <= 30) {
        toastr.options.timeOut = "3800";
    } else if (len > 30) {
        toastr.options.timeOut = "4800";
    }
    if(state == 1)
        toastr.success(content);
    else if(state == 2)
        toastr.error(content);
    else if(state == 3)
        toastr.warning(content);
    else
    toastr.info(content);
}


var userName = $('.username > input');
$('.switch span:first').on('click',function(){
    $(this).siblings().last().removeClass('active');
    $(this).addClass('active');
    userName.attr('placeholder','学号')
});
$('.switch span:last').on('click',function(){
    $(this).siblings().first().removeClass('active');
    $(this).addClass('active');
    userName.attr('placeholder','教师编号')
});

$('.btn-lgn').on('click',function(){
    userPassword = $('.password > input');
    if($('.switch > span').eq(0).attr('class') == 'active')
    {
        if(userName.val() == '')
            messageBox('用户名不能为空！','2')
        else if(userPassword.val() == '')
            messageBox('密码不能为空！','2')
        else{
            var user = {
                userName:userName.val(),
                userPassword:userPassword.val()
            };
            $.ajax({
                type: 'POST',
                url: "/login",
                data: JSON.stringify(user), 
                contentType: 'application/json; charset=UTF-8',
                dataType: 'json', 
                success: function(user) {
            		console.log("传输登录数据成功");
            		console.log(user);
            		if (user['success'] == "1")
            		    location.reload(true);
            		else
            		    messageBox('密码错误！','2');
                },
                error: function(data) {
            		console.log("传输登录数据失败");
            		location.reload(true);
                }
            });
        }
    }
    else if($('.switch > span').eq(2).attr('class') == 'active')
    {
        if(userName.val() == '')
        messageBox('教师编号不能为空！','2')
        else if(userPassword.val() == '')
        messageBox('密码不能为空！','2')
        else{
        var loginData = {
            userName:userName.val(),
            userPassword:userPassword.val()
        };
        $.ajax({
            url:"/loginteacher",
            type:"post",
            contentType:'application/json; charset=UTF-8',
            data:JSON.stringify(loginData),
            dataType:'json',
            cache:false,
            success:function (user) {
               if (user['success'] == "1")
                   location.reload(true);
               else
                   messageBox('密码错误！','2');
            },
            error:function (data) {
                location.reload(true);
                
            }
        })
    }
    }
});


$('.btn-reg').click(function () {
    var studentNo = $('.reg-no');
    var userName = $('.reg-name');
    var password = $('.reg-password');
    var confirmPassowrd = $('.reg-confirmPassword');
    if(!(/^20\d\d\d\d\d\d\d\d\d$/.test(studentNo.val())))
        messageBox('学号格式不对！','2')
    else if(userName.val() == '')
        messageBox('用户名不能为空！','2')
    else if(!(/^.*(?=.{6,})(?=.*\d)(?=.*[A-Z])(?=.*[a-z])(?=.*[!@#$%^&*? ]).*$/.test(password.val())))
        messageBox('密码太简单，最少6位，包括至少1个大写字母，1个小写字母，1个数字，1个特殊字符','2')
    else if(password.val() != confirmPassowrd.val())
        messageBox('两次密码不同！','2')
    else{
        var register = {
            studentNo:studentNo.val(),
            userName:userName.val(),
            password:password.val(),
            confirmPassowrd:confirmPassowrd.val()
        };
        console.log(register)
        $.ajax({
            type: 'POST',
            url: "/register",
            data: JSON.stringify(register), 
            contentType: 'application/json; charset=UTF-8',
            dataType: 'json', 
            success: function(data) {
        		//console.log("传输注册数据成功");
				messageBox('注册成功！','1')
            },
            error: function() {
        		//console.log("传输注册数据失败");
				messageBox('账号或学号已被注册！','2')
            }
        });
    }
});

$('.forget').click(function () {
    messageBox('请携带学生证到教务处找回密码','4')
})